package com.siam.package_promotion.controller.member;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siam.package_common.entity.BasicData;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_common.constant.BasicResultCode;
import com.siam.package_promotion.entity.DeductionMemberRelation;
import com.siam.package_promotion.service.DeductionMemberRelationService;
import com.siam.package_promotion.vo.DeductionMemberRelationVo;
import com.siam.package_user.auth.cache.MemberSessionManager;
import com.siam.package_user.entity.Member;
import com.siam.package_user.util.TokenUtil;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/rest/member/deductionMemberRelation")
@Transactional(rollbackFor = Exception.class)
@Api(tags = "抵扣卷用关系接口", description = "DeductionMemberRelationController")
public class DeductionMemberRelationController {

    @Autowired
    private DeductionMemberRelationService deductionMemberRelationService;

    @Autowired
    private MemberSessionManager memberSessionManager;

    @ApiOperation(value = "抵扣卷用户关系列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "抵扣劵名称", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "deductionAmount", value = "面值金额", required = false, paramType = "query", dataType = "decimal"),
            @ApiImplicitParam(name = "memberId", value = "用户id", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "costAmount", value = "购买金额", required = false, paramType = "query", dataType = "decimal"),
            @ApiImplicitParam(name = "isUsed", value = "是否已经使用，0=未使用，1=已使用", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNo", value = "页码(值为-1不分页)", required = true, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query", dataType = "int", defaultValue = "20")
    })
    @PostMapping(value = "/list")
    public BasicResult list(@RequestBody @Validated(value = {}) DeductionMemberRelationVo deductionMemberRelationVo, HttpServletRequest request) {
        Member loginMember = memberSessionManager.getSession(TokenUtil.getToken());
        DeductionMemberRelation deductionMemberRelation = new DeductionMemberRelation();
        BeanUtil.copyProperties(deductionMemberRelationVo, deductionMemberRelation, false);
        //查询当前登录用户持有的优惠券
        deductionMemberRelation.setMemberId(loginMember.getId());
        Page<DeductionMemberRelation> page = new Page(deductionMemberRelationVo.getPageNo(), deductionMemberRelationVo.getPageSize());
        deductionMemberRelationVo.setPageNo(null);
        deductionMemberRelationVo.setPageSize(null);
        Page<DeductionMemberRelation> pageRetrun = deductionMemberRelationService.getListByPage(page, deductionMemberRelation);
        
        return BasicResult.success(pageRetrun);
    }

    @ApiOperation(value = "查询抵扣卷数量")
    @PostMapping(value = "/selectCounts")
    public BasicResult selectCounts( HttpServletRequest request,@RequestParam("isUsed")Integer isUsed){
        BasicData basicResult = new BasicData();
        Member loginMember = memberSessionManager.getSession(TokenUtil.getToken());
        Integer memberId = loginMember.getId();

        Integer counts = deductionMemberRelationService.getCountsByMemberId(memberId,isUsed);

        basicResult.setData(counts);
        basicResult.setSuccess(true);
        basicResult.setCode(BasicResultCode.SUCCESS);
        basicResult.setMessage("获取成功");
        return basicResult;
    }

}